// JavaScript Document
((function(a){
	//插件本体
	var KuoqiTouch = function(opts){
		var ths = this;
		this.opts = a.kuoqiJs.opts(opts,{
			element:null,
			zoom_minlen:2,//缩放触发的最小刻度
			click_timelen:200,//一次点击的最大时间间隔
		},'element');
		this.element = this.opts.element;
		this.isclick = 0;//是否正在被点击，正在被点击则展示为被点击的计时器的唯一标识
		//当前活动的点的信息
		this.points = [];
		//一个点的相关信息，作为一个内部函数使用
		this.Point = function(evt){
			this.identifier = evt.identifier;
			this.x = evt.screenX;
			this.y = evt.screenY;
			this.src_x = this.x;
			this.src_y = this.y;
			this.evt = evt;
			this.src = evt;
			this.dx = 0;
			this.dy = 0;
			//下一个点的相关信息
			this.next = function(evt){
				this.dx = evt.screenX - this.x;
				this.dy = evt.screenY - this.y;
				this.x = evt.screenX;
				this.y = evt.screenY;
				this.src_x = this.src.screenX;
				this.src_y = this.src.screenY;
				this.src = this.evt;
				this.evt = evt;
			}
		}
		if(this.element==null){
			a.kuoqiJs.error("请传入element元素进行后续操作");return;	
		}
		this.onZoom = null;//缩放执行的方法
		this.onMove = null;//使用移动的执行方法
		this.onClick = null;//执行点击的方法
		
		//手机端缩放为两个按钮进行一个方法或者缩小
		this.doZoom = function(){
			this.doZoomMobile();
		}
		//执行点击的方法
		this.doClick = function(){
			this.doClickMobile();	
		}
		this.doMove = function(){
			this.doMoveMobile();
		}
		this.doZoomMobile = function(){
			var p1 = this.points[0];
			var p2 = this.points[1];
			var zoom = {};
			zoom.dst_x = p2.x - p1.x;
			zoom.dst_y = p2.y - p1.y;
			zoom.src_x = p2.src_x - p1.src_x;
			zoom.src_y = p2.src_y - p1.src_y;
			zoom.dst = Math.sqrt(zoom.dst_x * zoom.dst_x + zoom.dst_y * zoom.dst_y);
			zoom.src = Math.sqrt(zoom.src_x * zoom.src_x + zoom.src_y * zoom.src_y);
			zoom.dst_cx = (p2.x + p1.x) / 2;
			zoom.dst_cy = (p2.y + p1.y) / 2;
			zoom.src_cx = (p2.src_x + p1.src_x) / 2;
			zoom.src_cy = (p2.src_y + p1.src_y) / 2;
			zoom.points = this.points;
			if(Math.abs(zoom.src - zoom.dst)>ths.opts.zoom_minlen){
				this.onZoom&&this.onZoom(zoom);
			}
			//$Q.debug(zoom,"zoom");
		}
		this.doMoveMobile = function(){
			this.onMove&&this.onMove(this.points[0]);
		}
		this.doClickMobile = function(){
			this.onClick&&this.onClick(this.points[0]);	
		}
		
		this.preload = function(){
			var element = this.element;
			a.kuoqiJs(element).on('touchstart',function(rs){
				//阻止事件的默认操作
				//rs.cancelable = false;
				//rs.preventDefault();
				for(var i = 0;i<rs.changedTouches.length;i++){
					//这个将信息写入到当前列表中
					var point = new ths.Point(rs.changedTouches[i]);
					ths.points.push(point);
					//$Q.debug(point,"开始接触");
					//$Q.debug(rs,"srcElement");
				}
				if(ths.points.length==1){
					//触发
					ths.isclick = setTimeout(function(){
						//关闭当前的点击计时器，不做任何处理
						ths.isclick = 0;	
					},ths.opts.click_timelen);
				}
			});
			a.kuoqiJs(element).on('touchmove',function(rs){
				//阻止事件的默认操作
				rs.preventDefault();
				//当前的点的信息
				for(var i=0;i<rs.changedTouches.length;i++){
					for(var j=0;j<ths.points.length;j++){
						if(ths.points[i].identifier===rs.changedTouches[i].identifier){
							ths.points[i].next(rs.changedTouches[i]);	
						}	
					}	
				}
				//$Q.debug(ths.points.length,"haspoint");
				//这里进行一个放大缩小的判定
				if(ths.points.length==2){
					ths.doZoom();//两个手指的操作，当做是放大缩小
				}else if(ths.points.length==1){
					ths.doMove();//使用移动的功能	
				}
				
				//在移动过程中出去点击的功能
				if(ths.points.length==1){
					if(ths.isclick){
						//这里判定为执行一次点击
						clearTimeout(ths.isclick);
						ths.isclick = 0;	
					}
				}
				
				//$Q.debug(ths.points,"screenx");
			});
			a.kuoqiJs(element).on('touchend',function(rs){
				//阻止事件的默认操作
				//rs.cancelable = false;
				//rs.preventDefault();
				//判定当前的信息
				if(ths.points.length==1){
					if(ths.isclick){
						//这里判定为执行一次点击
						ths.doClick();
						clearTimeout(ths.isclick);
						ths.isclick = 0;	
					}
				}
				
				
				//对points进行一个反馈，使用touches
				for(var i = 0;i<rs.changedTouches.length;i++){
					//这个将信息写入到当前列表中
					var p1 = rs.changedTouches[i];
					var lis = [];
					for(var j=0;j<ths.points.length;j++){
						if(ths.points[j].identifier===p1.identifier){
							//判定为同一个点
							ths.points.splice(j,1);
							//$Q.debug(p1,"结束接触");
						}
					}
				}
			});
		}
	}
	a.kuoqiJs.plugIn.register('Touch',KuoqiTouch);
})(this))